﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ProjectList : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_Project(0);
            }
        }

        protected void DataBind_Project(int recordcount)
        {

            if (recordcount == 0)
            {
                this.AspNetPager_Content.CurrentPageIndex = 1;
            }

            int count = recordcount;
            int pageIndex = this.AspNetPager_Content.CurrentPageIndex;
            int pageSize = this.AspNetPager_Content.PageSize;
            if (Session["MerchantId"] != null)
            {
                if (ddlStatus.SelectedValue == "0")
                {
                    this.rpProjectList.DataSource = ServiceLocator.IProjectService.GetNxProjectCollection(int.Parse(Session["MerchantId"].ToString()), pageIndex, pageSize, ref count);
                }
                else
                {
                    this.rpProjectList.DataSource = ServiceLocator.IProjectService.GetNxProjectCollectionByStatus(int.Parse(Session["MerchantId"].ToString()), int.Parse(ddlStatus.SelectedValue), pageIndex, pageSize, ref count);
                }
            }
            else
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default","PLEASE_LOGIN_AGAIN") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','Login.aspx')", this.Page);
            }
            
            this.rpProjectList.DataBind();
            this.AspNetPager_Content.RecordCount = count;
        }

        protected void rpProjectList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NxProject nxProject = (NxProject)e.Item.DataItem;
                int projectId = nxProject.ProjectId;

                Label lbNum = (Label)e.Item.FindControl("lbNum");
                lbNum.Text = (e.Item.ItemIndex + 1).ToString();
                Label lbProjectName = (Label)e.Item.FindControl("lbProjectName");
                lbProjectName.Text = nxProject.ProjectName;
                Label lbSendNum = (Label)e.Item.FindControl("lbSendNum");
                lbSendNum.Text = (ServiceLocator.ILogService.GetSendNumByProjectId(projectId)).ToString();
                Label lbUseNum = (Label)e.Item.FindControl("lbUseNum");
                lbUseNum.Text = (ServiceLocator.ILogService.GetUseNumByProjectId(projectId)).ToString();
                Label lbStatus = (Label)e.Item.FindControl("lbStatus");
                switch (nxProject.Status)
                {
                    case 1: lbStatus.Text = (String)GetGlobalResourceObject("Default", "UNPUBLISHED_UNAUDITED"); break;
                    case 2: lbStatus.Text = (String)GetGlobalResourceObject("Default", "PUBLISHED_UNAUDITED"); break;
                    case 3: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDING"); break;
                    case 4: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_SUCCESS"); break;
                    case 5: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_FAILED"); break;
                }
                
                HyperLink hlPublish = (HyperLink)e.Item.FindControl("hlPublish");
                if (nxProject.Status != 1)
                {
                    hlPublish.Visible = false;
                }
                hlPublish.NavigateUrl = string.Format("Operate.aspx?ProjectId={0}&OpType=ProjectPublish", nxProject.ProjectId);
                HyperLink hlDetails = (HyperLink)e.Item.FindControl("hlDetails");
                hlDetails.NavigateUrl = string.Format("ProjectDetails.aspx?ProjectId={0}", nxProject.ProjectId);
                HyperLink hlModify = (HyperLink)e.Item.FindControl("hlModify");
                if (nxProject.Status != 1)
                {
                    hlModify.Visible = false;
                }
                hlModify.NavigateUrl = string.Format("ModifyProject.aspx?ProjectId={0}", nxProject.ProjectId);
                HyperLink hlDel = (HyperLink)e.Item.FindControl("hlDel");
                if (nxProject.Status != 1)
                {
                    hlDel.Visible = false;
                }
                hlDel.NavigateUrl = string.Format("Operate.aspx?ProjectId={0}&OpType=ProjectDel", nxProject.ProjectId);
                HyperLink hlSendDetails = (HyperLink)e.Item.FindControl("hlSendDetails");
                HyperLink hlUseDetails = (HyperLink)e.Item.FindControl("hlUseDetails");
                if (nxProject.Status == 4)
                {
                    hlSendDetails.Visible = true;
                    if (lbUseNum.Text != "0")
                    {
                        hlUseDetails.Visible = true;
                    }
                }
                hlSendDetails.NavigateUrl = string.Format("SendDetails.aspx?ProjectId={0}", nxProject.ProjectId);
                hlUseDetails.NavigateUrl = string.Format("UseDetails.aspx?ProjectId={0}", nxProject.ProjectId);
            }
        }

        protected void AspNetPager_Content_PageChanged(object sender, EventArgs e)
        {
            DataBind_Project(this.AspNetPager_Content.RecordCount);
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBind_Project(0);
        }
    }
}
